<?php

use common\components\helpers\Fun;
use yii\widgets\ActiveForm;
use yii\helpers\Html;
use common\components\extenders\MenuManage;
use doumiaoduo\layer\LayerIframeWidget;
use doumiaoduo\layer\LayerAsset;

LayerAsset::register($this);

LayerIframeWidget::widget(['button' => "['添加','取消']"]);
?>
    <div class="yx-cnt-box yx-radius2">
    <div class="yx-praise-box">
        <div class="yx-search-class">
            <a href="<?= Fun::url(['customer/details', 'id' => \Yii::$app->request->get('id')]) ?>">
                <button type="button" class="btn yx-back-list yx-radius5 yx-width110">返回上级</button>
            </a>
            <div class="yx-bgf7 yx-height108">
                <h4 class="yx-teacher-praise yx-floatl ">会员姓名：<?= $customer->name ?></h4>
                <h4 class="yx-teacher-praise yx-pl20 yx-floatl">当前可用余额：<span
                            class="yx-color2cb4f3 yx-font16"><?= isset($account->cash) ? $account->cash : 0 ?></span>元
                </h4>
                <div class="yx-clear"></div>
            </div>
            <div class="yx-search-prompt">
                <div class="yx-search-prompt-con yx-floatl">
                    <p class="yx-search-prompt-para">1. 搜索：选择类型或商品名称，点击搜索按钮；</p>
                    <p class="yx-search-prompt-para">2. 添加商品：选择商品（若搜索不到可在类型或商品名称中选择其一，重新搜索），选择数量，点击添加商品按钮；</p>
                    <p class="yx-search-prompt-para">3. 若在下面的列表中出现了您所选的商品，即说明添加成功；</p>
                </div>
                <div class="yx-how-search yx-floatr">
                    <h3 class="yx-how-search-text">如何搜索、添加商品？</h3>
                    <span class="yx-look-how-search yx-radius2">查看</span>
                </div>
                <div class="yx-clear"></div>
            </div>
            <div>
                <div class="yx-class-box yx-pt18 yx-pb0">
                    <div class="yx-search-goods-box">


                            <span class="yx-order-number">1</span>
                            <div class="yx-form-group">
                                <div class="yx-floatl yx-ml6">
                                    <label class="control-label yx-floatl zx-lh28 ">类型：</label>
                                    <div class="yx-floatl">
                                        <div class="form-group">
                                            <select name="type_id">
                                                <option value="">选择类型</option>
                                                <option value="2">乐器超市</option>
                                                <option value="4">文化超市</option>
                                                <option value="6">水吧超市</option>
                                            </select>
                                        </div>
                                    </div>
                                </div>
                                <div class="yx-floatl yx-ml6">
                                    <label class="zx-lh28">商品名称：</label>
                                    <input type="text" name="goods_name" value="">
                                </div>
                                <button type="button" id="btn" class="btn yx-search yx-radius2">搜索</button>
                            </div>
                        </div>
                        <div class="yx-search-goods-box">
                            <span class="yx-order-number">2</span>
                            <div class="yx-form-group">
                                <div class="yx-floatl yx-ml6">
                                    <label class="control-label yx-floatl zx-lh28 ">商品：</label>
                                    <select name="goods" id="goods_list">
                                        <option value=""></option>
                                    </select>
                                </div>
                                <div class="yx-floatl yx-ml6">
                                    <label class="zx-lh28">数量：</label>
                                    <span class="dm" onclick="qtychange('-');">-</span><input name="num" value="1"
                                                                                              id="qty" type="text"
                                                                                              style="width: 50px; text-align: center"><span
                                            class="dm" onclick="qtychange('+');">+</span>
                                </div>
                                <button type="button" id="addcart" class="btn yx-search yx-radius2">添加商品</button>
                            </div>
                        </div>
                    </div>
                    <div class="yx-search-goods-result">
                        <span class="yx-order-number">3</span>
                        <div class="docs-main">
                            <table class="tablesaw table yx-table1 yx-table3 yx-table5 table-striped with-check"
                                   id="table-list">
                                <thead>
                                <tr class="yx-bgd2f6ec">
                                    <th>商品名称</th>
                                    <th>价格</th>
                                    <th>库存</th>
                                    <th>数量</th>
                                    <th>小计</th>
                                    <th>操作</th>
                                </tr>
                                </thead>

                            </table>

                        </div>
                        <div class="yx-clear"></div>
                    </div>
                </div>
            </div>
            <div class="yx-class-box yx-bt" style="padding-top: 10px">
                <?php $form = ActiveForm::begin(['enableAjaxValidation' => true, 'enableClientValidation' => false, 'options' => ['class' => 'form-validate form-horizontal']]); ?>
                <div class="control-group" style="display: none">
                    <label class="control-label control-label3 yx-mr25" style="padding-top:0;"
                           for="inputTime"><?= $model->attributeLabels()['total_amount']; ?></label>
                    <div class="controls">
                        <?= $form->field($model, 'total_amount', ['template' => '{input}{error}'])->hiddenInput(); ?>
                    </div>
                </div>

                <div class="control-group" style="display: none">
                    <label class="control-label control-label3 yx-mr25" style="padding-top:0;"
                           for="inputTime"><?= $model->attributeLabels()['item_num']; ?></label>
                    <div class="controls">
                        <?= $form->field($model, 'item_num', ['template' => '{input}{error}'])->hiddenInput(); ?>
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label control-label3 yx-mr25" style="padding-top:0;"
                           for="inputTime">合计金额</label>
                    <div class="controls">
                        <span class="yx-font18 yx-color4f4f4f" style="line-height:30px;" id="amount">0.00</span>
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label control-label3 yx-mr25" style="padding-top:0;"
                           for="inputTime">选择优惠券</label>
                    <div class="controls">
                        <select>
                            <option>请选择</option>
                        </select>
                    </div>
                </div>
                <div class="control-group yx-mb0">
                    <label class="control-label control-label3 yx-mr25" style="padding-top:0;"
                           for="inputTime"><?= $model->attributeLabels()['final_amount']; ?></label>
                    <div class="controls">
                        <?= $form->field($model, 'final_amount', ['template' => '{input}{error}{hint}'])->textInput(['style' => 'width:220px', 'readonly' => 'true', 'value' => 0, 'class' => 'input-text']) ?>
                    </div>
                </div>
                <div class="control-group yx-mb0">
                    <label class="control-label control-label3 yx-mr25" style="padding-top:0;"
                           for="inputTime">商品编号</label>
                    <div class="controls">
                        <?= $form->field($model, 'goods_id', ['template' => '{input}{error}'])->textInput(['style' => 'width:220px', 'class' => 'input-text', 'readonly' => true]); ?>
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label control-label3 yx-mr25" style="padding-top:0;"
                           for="inputTime"><?= $model->attributeLabels()['remarks']; ?></label>
                    <div class="controls">
                        <?= $form->field($model, 'remarks', ['template' => '{input}{error}{hint}'])->textarea(['rows' => 2, 'style' => 'width:220px', 'class' => 'textarea_editor yx-width510 yx-height56']) ?>
                    </div>
                </div>

                <div class="yx-button-group">
                    <button type="submit" id="dosubmit" class="btn yx-save yx-radius50">提交</button>
                    <a href="<?= Fun::url(['customer/details', 'id' => \Yii::$app->request->get('id')]) ?>"
                       class="btn yx-close1 yx-radius50 yx-ml4" style="display: inline-block">返回</a>
                </div>
                <?php ActiveForm::end(); ?>
            </div>


        </div>
    </div>
    <input type="hidden" id="search_url" value="<?= Fun::url(['customer/search']) ?>">
    <input type="hidden" id="goods_url" value="<?= Fun::url(['customer/goodsinfo']) ?>">
    <input type="hidden" id="remove_url" value="<?= Fun::url(['customer/remove']) ?>">
<?php
$js = <<<JS
   //搜索商品
   $('#btn').on('click',function(){
       var type_id = $('select[name="type_id"]').val();
       var name = $('input[name="goods_name"]').val();
       var url = $('#search_url').val();
       $.post(url,{type_id:type_id,goods_name:name},function(datas) {
          var goods_list = datas.data;
          var str = '<option value="">请选择商品</option>';
          if(goods_list != null){
              for (var i=0;i<goods_list.length;i++){
                  var goods = goods_list[i];
                  str += '<option value="'+goods.id+'">'+goods.name+'</option>';
              }   
              $('#goods_list').html(str);
          }else{
              alert('没有此商品！');
          }
       },'json');
   });

  $('#addcart').on('click',function(){
      var goods_id = $('#goods_list').val();
      var num = parseInt($('input[name="num"]').val());
      if(goods_id.length == 0){
          alert('请先选择商品！');
          return false;
      }
      var url = $('#goods_url').val();
      $.post(url,{goods_id:goods_id,num:num},function(datas) {
          if(datas.data != null){
              var goods = datas.data;
              var str = '';
              for (var i=0;i<goods.length;i++){
                  var good = goods[i];
                  str += '<tr>';
                  str += '<td>'+good.name+'</td>';
                  str += '<td>'+good.price+'</td>';
                  str += '<td>'+good.store+'</td>';
                  str += '<td>'+good.num+'</td>';
                  str += '<td>'+good.subtotal+'</td>';
                  str += '<td><button type="" class="remove btn yx-classmagage-about yx-bg7ecef4 yx-width67 yx-radius2 yx-mr5" data-id="'+good.id+'">移除</button></td>';
                  str +='</tr>';
              }
              $("#table-list tr").not(":eq(0)").remove();
              $('#table-list').append(str);
              $('#orders-final_amount').val(datas.content);
              $('#amount').html(datas.content);
              $('#orders-goods_id').val(datas.pages);
          }
      },'json');
      
  });
  
  $(document).on('click','.remove',function(){
      var _this = $(this);
      var id = _this.data('id');
      var url = $('#remove_url').val();
      $.post(url,{id:id},function(json) {
          _this.parents('tr').remove();
          $('#orders-final_amount').val(json.content);
          $('#amount').html(json.content);
          $('#orders-goods_id').val(json.pages);
        
      },'json');
      
  });
   //会员购买商品页面操作提示
$(function () {
    $(".yx-look-how-search").click(function () {
        console.log("nihao");
        if($(this).parent().prev().css("display") === "none") {
            $(this).parent().prev().show();
            $(this).text('关闭');

        }else{
            $(this).parent().prev().hide();
            $(this).text('查看');
        }
    });
})


JS;
\Yii::$app->view->registerJs($js);
?>